package com.unigator;

import java.util.ArrayList;
import java.util.List;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.unigator.MainActivity.transData;
import com.unigator.ServerIO.eFunc;

import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Dialog;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;


public class Map extends FragmentActivity implements LocationListener {
	 
	private Polyline polyLine;
	private PolylineOptions rectOptions = new PolylineOptions();
    GoogleMap googleMap;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        
        Intent intent = getIntent();
        int src_id = intent.getIntExtra("src_id", 0);
        System.out.println(src_id);
        int dst_id = intent.getIntExtra("dst_id", 0);
        System.out.println(dst_id);
        
        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());
 
        // Showing status
        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
 
            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();
 
        }else { // Google Play Services are available
 
            // Getting reference to the SupportMapFragment of activity_main.xml
            SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
 
            // Getting GoogleMap object from the fragment
            googleMap = fm.getMap();
 
            // Enabling MyLocation Layer of Google Map
            //googleMap.setMyLocationEnabled(true);
 
            // Getting LocationManager object from System Service LOCATION_SERVICE
            LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
 
            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();
 
            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);
 
            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);
<<<<<<< HEAD
            
            //add marker for current location
            //addMarkers(googleMap,location.getLatitude() ,location.getLongitude());
            
            //locationManager.requestLocationUpdates(provider, 20000, 0, this);
            
            new transData().execute(eFunc.FindPathInt,1,5);
            
            //if(location!=null){
            	//onLocationChanged(location);
            	//addMarkers(googleMap,location.getLatitude() ,location.getLongitude());
            //}
            
=======
 
            if(location!=null){
            	onLocationChanged(location);
            	//addMarkers(googleMap,location.getLatitude() ,location.getLongitude());
            }
            //locationManager.requestLocationUpdates(provider, 20000, 0, this);
            locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 2000, 0, this);
>>>>>>> 9a456f07899b24566de210037bc575d047e37e32
        }
    }
    @Override
    public void onLocationChanged(Location location) {
 
        
        // Getting latitude of the current location
        double latitude = location.getLatitude();
 
        // Getting longitude of the current location
        double longitude = location.getLongitude();

        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);
        
        addMarkers(googleMap,location.getLatitude() ,location.getLongitude());
        
         // Showing the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
 
        // Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
        
        
    }
 
    private void addMarkers(GoogleMap googleMap,double latitude ,double longitude)
    {
    	googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)).title(""+0));
    }
    
    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub
    }
 
    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub
    }
    
    public class transData extends ServerIO
    {
    	transData()
    	{
    		super();
    	}
    	
    	@Override
    	protected void onPostExecute(Object object)
    	{
    		RetObject retObject=(RetObject)object;
    		eFunc func=retObject.func;
    		switch(func)
    		{
    			case FindPathInt:
    			{
    				ArrayList<MyLocation> locationList=(ArrayList<MyLocation>)retObject.retData;
    				polyLine = googleMap.addPolyline(rectOptions);
    				List<LatLng> points = polyLine.getPoints();
    				for(int i=0; i< locationList.size(); i++)
    				{
    					points.add(new LatLng(locationList.get(i).latitude, locationList.get(i).longitude));
    				}
    				polyLine.setPoints(points);
    				break;
    			}
    			case FindPathDouble:
    			{
    				ArrayList<MyLocation> locationList=(ArrayList<MyLocation>)retObject.retData;
    	    		MyLocation location=locationList.get(0);
    				break;
    			}
 
    		}
    		
    		
    	}
    }
}

